package com.neighbor.neighborhooduser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neighbor.neighborhooduser.pojo.UserFriends;
import com.neighbor.neighborhooduser.pojo.Users;
import com.neighbor.neighborhooduser.service.IUserFriendsService;
import com.neighbor.neighborhooduser.utils.ReOpe;
import com.neighbor.neighborhooduser.utils.ResponseMsg;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户好友关系表 前端控制器
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:13:04
 */
@RestController
@RequestMapping("/userFriends")
public class UserFriendsController {
    @Autowired
    private IUserFriendsService userFriendsService;
    @Autowired
    ReOpe reOpe;    //方法
    //是否关注了别人
    @RequestMapping("isFollow")
    ResponseMsg isFollow(Integer user_id,Integer friend_id){
        if(userFriendsService.isFollow(user_id, friend_id)==1){
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"true");
        }
        return ResponseMsg.getInstance(ResponseMsg.PERMISSIONS_ERROR,"false");
    }
    //关注别人
    @RequestMapping("follow/{user_id}/{friend_id}")
    ResponseMsg follow(@PathVariable Integer user_id,
                       @PathVariable Integer friend_id) {
        if (userFriendsService.follow(user_id, friend_id) == 1) {
            return ResponseMsg.success();
        }
        return ResponseMsg.getInstance(ResponseMsg.DATA_ERROR, "错误");
    }
    //取关别人
    @RequestMapping("unFollow/{user_id}/{friend_id}")
    ResponseMsg unFollow(@PathVariable Integer user_id,
                       @PathVariable Integer friend_id) {
        if (userFriendsService.unFollow(user_id, friend_id) == 1) {
            return ResponseMsg.success();
        }
        return ResponseMsg.getInstance(ResponseMsg.DATA_ERROR, "错误");
    }

    /**
     *      根据请求体获取用户
     * @param request   请求体
     * @return  好友用户ID
     */
    @RequestMapping("selFriendsById")
    ResponseMsg selFriendsById(HttpServletRequest request){
        //验证token
        ResponseMsg responseMsg = reOpe.IsToken(request);
        if(responseMsg.getCode() == ResponseMsg.SUCCESS) {
            Users users = ((Users) responseMsg.getData());
            QueryWrapper<UserFriends> wrapper = new QueryWrapper<>();
            //sql语句条件查询
            wrapper.eq("user_id", users.getUserId()).select("friend_id");
            List<UserFriends> userFriends = userFriendsService.list(wrapper);
            //处理结果集
            List<Integer> friendsId = userFriends.stream()
                    .map(UserFriends::getFriendId)
                    .toList();
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "获取到好友信息",friendsId);
        }
        return responseMsg;
    }
}
